package com.collect.tanzania.zhiScene.zhiRemind.zhiCustomer.zhiAdmin.zhiFollow;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.collect.tanzania.R;
import com.collect.tanzania.zhiAdater.zhiCustomerOrder.ZHICustomerOrderAdjuster;
import com.collect.tanzania.zhiBase.BaseFragment;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRCustomerBankAccountEntity;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRCustomerRepayOrderListEntity;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRPartRepayListEntity;
import com.collect.tanzania.zhiBean.zhiSend.ZHISCustomerMarkedOrder;
import com.collect.tanzania.zhiBean.zhiSend.ZHISCustomerRepayOrderListEntity;
import com.collect.tanzania.zhiBean.zhiSend.ZHISPartRepayListEntity;
import com.collect.tanzania.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.tanzania.zhiScene.zhiRemind.zhiCustomer.zhiAdmin.zhiRoot.ZHIRootCustomerAdminFragmentInterface;
import com.collect.tanzania.zhiScene.zhiRemind.zhiCustomer.zhiMember.zhiRoot.ZHICustomerOrderType;
import com.collect.tanzania.zhiScene.zhiWidget.zhiDialog.ZHIDialogPartRepay;
import com.collect.tanzania.zhiScene.zhiWidget.zhiNavigationBar.ZHITopTabBar;
import com.collect.tanzania.zhiScene.zhiWidget.zhiSubmitRecTagView.ZHISubmitRecFollowView;
import com.collect.tanzania.zhiUtils.ToastUtil;

import java.util.ArrayList;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class ZHICustomerFollowMixAdminFragment extends BaseFragment<ZHICustomerFollowAdminPresenter> implements ZHICustomerFollowAdminContract.View {

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.tv_page_title)
    TextView tv_page_title;

    @BindView(R.id.tbl_segment)
    ZHITopTabBar tbl_segment;

    @BindView(R.id.rc_list)
    RecyclerView rc_list;

    @BindView(R.id.iv_filter)
    ImageView iv_filter;
    @BindView(R.id.iv_products)
    ImageView iv_products;

    @BindView(R.id.tv_query_count)
    TextView tv_query_count;

    private int mOrderType = ZHICustomerOrderType.TYPE_TODAY;

    private ZHISubmitRecFollowView mZHISubmitRecFollowView;

    private ZHIRootCustomerAdminFragmentInterface mZHIRootCustomerAdminFragmentInterface;

    private final ZHICustomerOrderAdjuster mZHICustomerOrderAdjuster = new ZHICustomerOrderAdjuster();

    private ArrayList<ZHIRCustomerRepayOrderListEntity> mCustomerRepayOrderListEntityArrayList;

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_customer_follow_mix;
    }

    @Override
    protected void initView(View view) {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));
            mZHISubmitRecFollowView = new ZHISubmitRecFollowView(getActivity());
            mZHISubmitRecFollowView.setSubmitRecFollowItemClickListener(ctsCustomerMarkedOrder -> {
                if (ctsCustomerMarkedOrder != null && mPresenter != null) {
                    mPresenter.markCustomerRepayOrder(getActivity(), ctsCustomerMarkedOrder, true);
                }
            });

            iv_filter.setOnClickListener(v -> showFilter());
            iv_products.setOnClickListener(v -> ZHIProjectRouter.toWordsSkills());

            mOrderType = tbl_segment.getCurIndex();
            tbl_segment.setZHITabBarItemClickListener(index -> {
                mOrderType = index;
                initData(true);
            });

            initEvent();
            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        try {
            mZHIRootCustomerAdminFragmentInterface = (ZHIRootCustomerAdminFragmentInterface) activity;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onGetLatestCustomerRepayOrderListV2(ArrayList<ZHIRCustomerRepayOrderListEntity> zhirRCustomerRepayOrderListEntityArrayList) {
        try {
            srl_swipe_layout.setRefreshing(false);
            mCustomerRepayOrderListEntityArrayList = zhirRCustomerRepayOrderListEntityArrayList;
            mZHICustomerOrderAdjuster.setNewData(mCustomerRepayOrderListEntityArrayList);
            tv_page_title.setText("Follow(" + mCustomerRepayOrderListEntityArrayList.size() + ")");
            tv_query_count.setText("Total:" + mCustomerRepayOrderListEntityArrayList.size());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetPartRepayList(ArrayList<ZHIRPartRepayListEntity> arrayList) {
        try {
            if (arrayList != null && arrayList.size() > 0) {
                ZHIDialogPartRepay.showDataSource(getActivity(), arrayList);
            } else {
                ToastUtil.s("No partial repayment data yet.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetUserVirtualAccount(ZHISPartRepayListEntity zhiSPartRepayListEntity, ZHIRCustomerBankAccountEntity zhirCustomerBankAccountEntity) {
        try {
            if (mCustomerRepayOrderListEntityArrayList != null) {
                int mposition = -1;
                for (int i = 0; i < mCustomerRepayOrderListEntityArrayList.size(); i++) {
                    ZHIRCustomerRepayOrderListEntity zhirCustomerRepayOrderListEntity = mCustomerRepayOrderListEntityArrayList.get(i);
                    if (zhirCustomerRepayOrderListEntity.getOrderId().equalsIgnoreCase(zhiSPartRepayListEntity.getOrderId())) {
                        mposition = i;
                        zhirCustomerRepayOrderListEntity.setVirtualBankName(zhirCustomerBankAccountEntity.getBankName());
                        zhirCustomerRepayOrderListEntity.setVirtualBankAccount(zhirCustomerBankAccountEntity.getBankAccount());
                    }
                }

                if (mposition >= 0) {
                    Log.d(TAG, "onGetUserVirtualAccount: "+mposition);
                    mZHICustomerOrderAdjuster.refreshNotifyItemChanged(mposition);
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onMarkCustomerRepayOrder() {
        mZHISubmitRecFollowView.dismiss();
        initData(true);
    }

    @SuppressLint("InflateParams")
    public void initEvent() {
        try {
            srl_swipe_layout.setOnRefreshListener(() -> initData(false));
            mZHICustomerOrderAdjuster.setEmptyView(getRefreshEmptyView());
            rc_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
            rc_list.setAdapter(mZHICustomerOrderAdjuster);

            mZHICustomerOrderAdjuster.setOnCTCustomerOrderAdjusterItemClickListener((type, zhirRCustomerRepayOrderListEntity) -> {
                if (type == ZHICustomerOrderAdjuster.CustomerOrderAdjusterItemClickType.TYPE_WHATSAPP) {
                    if (mPresenter != null) {
                        ZHISCustomerMarkedOrder zhiSCustomerMarkedOrder = new ZHISCustomerMarkedOrder();
                        zhiSCustomerMarkedOrder.setMobile(zhirRCustomerRepayOrderListEntity.getMobile());
                        zhiSCustomerMarkedOrder.setFullName(zhirRCustomerRepayOrderListEntity.getFullName());
                        zhiSCustomerMarkedOrder.setRemark("whatsapp");
                        zhiSCustomerMarkedOrder.setOrderId(zhirRCustomerRepayOrderListEntity.getOrderId());
                        zhiSCustomerMarkedOrder.setType(getCaseSearchParams().getType());
                        mPresenter.markCustomerRepayOrder(getActivity(), zhiSCustomerMarkedOrder, false);
                    }
                } else if (type == ZHICustomerOrderAdjuster.CustomerOrderAdjusterItemClickType.TYPE_REPAID) {
                    if (mPresenter != null) {
                        mPresenter.getPartRepayList(getActivity(), new ZHISPartRepayListEntity(zhirRCustomerRepayOrderListEntity.getOrderId()));
                    }
                } else if (type == ZHICustomerOrderAdjuster.CustomerOrderAdjusterItemClickType.TYPE_BANK_ACCOUNT) {
                    if (mPresenter != null) {
                        mPresenter.getUserVirtualAccount(getActivity(), new ZHISPartRepayListEntity(zhirRCustomerRepayOrderListEntity.getOrderId()));
                    }
                } else if (type == ZHICustomerOrderAdjuster.CustomerOrderAdjusterItemClickType.TYPE_FOLLOW) {
                    showSendSmsView(zhirRCustomerRepayOrderListEntity);
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initData(boolean showLoading) {
        if (mPresenter != null) {
            ZHISCustomerRepayOrderListEntity zhisCustomerRepayOrderListEntity;

            if (mOrderType == ZHICustomerOrderType.TYPE_TODAY) {
                zhisCustomerRepayOrderListEntity = getD0CaseSearchParams();
            } else {
                zhisCustomerRepayOrderListEntity = getD1CaseSearchParams();
            }

            if (zhisCustomerRepayOrderListEntity == null) {
                srl_swipe_layout.setRefreshing(false);
                return;
            }

            mPresenter.getLatestCustomerRepayOrderListV2(getActivity(), zhisCustomerRepayOrderListEntity, showLoading);
        }
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> {
            initData(true);
        });
        return emptyView;
    }

    @SuppressLint("InflateParams")
    private void showSendSmsView(ZHIRCustomerRepayOrderListEntity zhirRCustomerRepayOrderListEntity) {
        try {
            if (mZHISubmitRecFollowView == null) {
                mZHISubmitRecFollowView = new ZHISubmitRecFollowView(getActivity());
            }
            View parentView = LayoutInflater.from(getActivity()).inflate(R.layout.activity_root_customer_service, null);
            mZHISubmitRecFollowView.setmParentView(parentView);
            mZHISubmitRecFollowView.setName(zhirRCustomerRepayOrderListEntity.getFullName());
            mZHISubmitRecFollowView.setContact(zhirRCustomerRepayOrderListEntity.getMobile());
            mZHISubmitRecFollowView.setOrderId(zhirRCustomerRepayOrderListEntity.getOrderId());
            mZHISubmitRecFollowView.setType(getCaseSearchParams().getType());
            mZHISubmitRecFollowView.showAtLocation(parentView, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void refreshData() {
        initData(true);
    }

    public void showFilter() {
        if (mZHIRootCustomerAdminFragmentInterface != null) {
            mZHIRootCustomerAdminFragmentInterface.onShowFilterFollow();
        }
    }

    public ZHISCustomerRepayOrderListEntity getCaseSearchParams() {
        if (mZHIRootCustomerAdminFragmentInterface != null) {
            return mZHIRootCustomerAdminFragmentInterface.onGetFollowFilterParams();
        }
        return new ZHISCustomerRepayOrderListEntity();
    }

    public ZHISCustomerRepayOrderListEntity getD0CaseSearchParams() {
        if (mZHIRootCustomerAdminFragmentInterface != null) {
            return mZHIRootCustomerAdminFragmentInterface.onGetFollowFilterD0Params();
        }
        return new ZHISCustomerRepayOrderListEntity();
    }

    public ZHISCustomerRepayOrderListEntity getD1CaseSearchParams() {
        if (mZHIRootCustomerAdminFragmentInterface != null) {
            return mZHIRootCustomerAdminFragmentInterface.onGetFollowFilterD1Params();
        }
        return new ZHISCustomerRepayOrderListEntity();
    }
}